package com.yanyu.init.controller;

import com.google.gson.Gson;
import com.yanyu.init.annotation.AuthCheck;
import com.yanyu.init.common.BaseResponse;
import com.yanyu.init.common.ErrorCode;
import com.yanyu.init.common.ResultUtils;
import com.yanyu.init.constant.UserConstant;
import com.yanyu.init.exception.BusinessException;
import com.yanyu.init.mapper.FriendLinkMapper;
import com.yanyu.init.model.dto.flink.FriendAddRequest;
import com.yanyu.init.model.dto.flink.FriendDeleteRequest;
import com.yanyu.init.model.dto.flink.FriendUpdateRequest;
import com.yanyu.init.model.entity.FriendLink;
import com.yanyu.init.model.vo.FriendVO;
import com.yanyu.init.service.FriendLinkService;
import com.yanyu.init.utils.ThrowUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/flink")
@Slf4j
public class FriendController {

    @Resource
    private FriendLinkService friendService;

    @Resource
    private FriendLinkMapper friendLinkMapper;

    private final static Gson GSON = new Gson();

    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addFriendLink(@RequestBody FriendAddRequest friendAddRequest, HttpServletRequest request) {
        if (friendAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        FriendLink friendLink = new FriendLink();
        BeanUtils.copyProperties(friendAddRequest, friendLink);
        List<String> tags = friendAddRequest.getLinkTags();
        if (tags != null) {
            friendLink.setLinkTags(GSON.toJson(tags));
        }
        friendService.validFriend(friendLink, true);
        friendLink.setLinkRecommend(0L);//默认不推荐
        boolean result = friendService.save(friendLink);
        // 验证执行结果
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        Long friendLinkId = friendLink.getId();
        return ResultUtils.success(friendLinkId);
    }

    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteFriend(@RequestBody FriendDeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long deleteRequestId = deleteRequest.getId();
        // 判断友链是否存在
        FriendLink oldLink = friendService.getById(deleteRequestId);
        ThrowUtils.throwIf(oldLink == null, ErrorCode.NOT_FOUND_ERROR);
        boolean removeById = friendService.removeById(deleteRequestId);
        return ResultUtils.success(removeById);
    }

    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateFriend(@RequestBody FriendUpdateRequest updateRequest, HttpServletRequest request) {
        if (updateRequest == null || updateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        FriendLink friend = new FriendLink();
        BeanUtils.copyProperties(updateRequest, friend);
        List<String> tags = updateRequest.getLinkTags();
        if (tags != null) {
            friend.setLinkTags(GSON.toJson(tags));
        }
        friendService.validFriend(friend, true);
        Long id = friend.getId();
        FriendLink oldLink = friendService.getById(id);
        ThrowUtils.throwIf(oldLink == null, ErrorCode.NOT_FOUND_ERROR);
        boolean update = friendService.updateById(friend);
        return ResultUtils.success(update);
    }

    @GetMapping("/list/vo")
    public BaseResponse<List<FriendVO>> getFriendList() {
        List<FriendLink> friendLinkList = friendLinkMapper.selectList(null);
        List<FriendVO> friendVOList = friendLinkList.stream().map(friendLink -> friendService.getFriendVO(friendLink)).collect(Collectors.toList());
        return ResultUtils.success(friendVOList);
    }
}
